#!/bin/bash
KUBE_CONFIG_DIR=/opt/kubeconfig
CONFIG_FILE_NAME=kube-controller-manager.kubeconfig
export PATH=/opt/k8s/bin:$PATH

if [ ! -d ${KUBE_CONFIG_DIR} ]; then
    mkdir ${KUBE_CONFIG_DIR}
fi

echo "Setting cluster..."
kubectl config set-cluster kubernetes \
  --certificate-authority=/opt/cert/ca.pem \
  --embed-certs=true \
  --server="https://##NODE_IP##:6443" \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}

echo "Setting credentials..."
kubectl config set-credentials system:kube-controller-manager \
  --client-certificate=/opt/cert/kube-controller-manager.pem \
  --client-key=/opt/cert/kube-controller-manager-key.pem \
  --embed-certs=true \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}

echo "setting context"
kubectl config set-context system:kube-controller-manager \
  --cluster=kubernetes \
  --user=system:kube-controller-manager \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}

echo "Using context..."
kubectl config use-context system:kube-controller-manager \
  --kubeconfig=${KUBE_CONFIG_DIR}/${CONFIG_FILE_NAME}

